#region License
/*
 * Copyright (c) 2007, Matthew Holmes
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * * Neither the name of The Mango Project nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

using Mango.Components;
using Mango.Framework.Common;
using Mango.Framework.Tree;

namespace Mango.Dockers {
    public partial class ProjectExplorer : WeifenLuo.WinFormsUI.Docking.DockContent {
        private PropertyView m_PropView = null;
        private Project m_Project = null;
        private Dictionary<string, int> m_ImageIdxMap = new Dictionary<string, int> ();

        public ProjectExplorer (PropertyView pv) {
            InitializeComponent ();
            m_PropView = pv;
            this.Icon = Singleton.Ref<ImageManager> ().GetIcon ("workspace");
            m_GuiWorkspaceTree.ImageList = Singleton.Ref<ImageManager> ().ImageList;
        }

        public Project Project {
            get {
                return m_Project;
            }
            set {
                m_Project = value;
                UpdateView ();
            }
        }

        private void UpdateView () {
            m_GuiWorkspaceTree.Nodes.Clear ();

            ProjectNode proj = m_Project.ProjectRoot;
            ProjectTreeNode projNode = new ProjectTreeNode (
                m_Project,
                String.Format ("Project '{0}'", m_Project.Name),
                "workspace"
            );
            projNode.Tag = proj;

            FolderTreeNode node = new FolderTreeNode (
                m_Project,
                "Materials",
                "workspace.materialfolder",
                "workspace.openfolder"
            );
            projNode.Nodes.Add (node);

            /*foreach (MaterialNode mat in root.Materials) {
                ProjectTreeNode matNode = new ProjectTreeNode (mat.Name, "workspace.material");
                matNode.Tag = mat;
                node.Nodes.Add (matNode);

                foreach (TechniqueNode tech in mat.Techniques) {
                    ProjectTreeNode techNode = new ProjectTreeNode (tech.Name, "workspace.technique");
                    techNode.Tag = tech;
                    matNode.Nodes.Add (techNode);
                }
            }*/

            m_GuiWorkspaceTree.Nodes.Add (projNode);
            projNode.Expand ();
            m_GuiWorkspaceTree.SelectedNode = projNode;
        }

        private void m_GuiWorkspaceTree_AfterSelect (object sender, TreeViewEventArgs e) {
            m_PropView.ActiveObject = e.Node.Tag;
        }

        private void m_GuiWorkspaceTree_BeforeExpand (object sender, TreeViewCancelEventArgs e) {
            if (e.Node is FolderTreeNode) {
                ((FolderTreeNode)e.Node).Expanding ();
            }
        }

        private void m_GuiWorkspaceTree_BeforeCollapse (object sender, TreeViewCancelEventArgs e) {
            if (e.Node is FolderTreeNode) {
                ((FolderTreeNode)e.Node).Collapsing ();
            }
        }

        private void m_GuiWorkspaceTree_NodeMouseClick (object sender, TreeNodeMouseClickEventArgs e) {
            if (e.Button != MouseButtons.Right) {
                return;
            }

            if (m_GuiWorkspaceTree.SelectedNode != e.Node) {
                m_GuiWorkspaceTree.SelectedNode = e.Node;
            }

            if (e.Node is ProjectTreeNode) {
                bool showMenu = ((ProjectTreeNode)e.Node).InitializeMenu (m_ContextMenu);
                if (showMenu) {
                    m_ContextMenu.Show (m_GuiWorkspaceTree, e.X, e.Y);
                }
            }
        }
    }
}
